<template>
  <a-spin :spinning="pageLoading">
    <a-form-model class="detail-form" :model="form" :label-col="labelCol" :wrapper-col="wrapperCol">
      <a-form-model-item label="账户名">
        <a-input allowClear v-model="form.username"/>
      </a-form-model-item>
      <a-form-model-item label="姓名">
        <a-input allowClear v-model="form.name"/>
      </a-form-model-item>
      <a-form-model-item label="密码">
        <a-input allowClear type="password" v-model="form.password"/>
      </a-form-model-item>
      <a-form-model-item label="手机号">
        <a-input allowClear v-model="form.mobile"/>
      </a-form-model-item>
      <a-form-model-item label="所属角色">
        <a-select v-model="form.groupId" placeholder="请选择所属角色" :disabled="form.id == 1">
          <a-select-option :value="v.id" v-for="(v,i) in groupList" :key="i">{{ v.title }}</a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item :wrapper-col="{ span: 14, offset: 5 }">
        <a-button type="primary" @click="onSubmit"> 提交</a-button>
      </a-form-model-item>
    </a-form-model>
  </a-spin>
</template>
<script>
function getBase64(img, callback) {
  const reader = new FileReader();
  reader.addEventListener('load', () => callback(reader.result));
  reader.readAsDataURL(img);
}

export default {
  data() {
    return {
      pageLoading: false,
      uploadLoading: false,
      labelCol: {span: 5},
      wrapperCol: {span: 14},
      groupList: [],
      id: '',
      form: {
        username: '',
        groupId: "",
        password: '',
        name: "",
        mobile: "",
      },
    };
  },
  async created() {
    if (this.$route.query.id) {
      this.id = this.$route.query.id
      this.getData()
    }
    this.groupList = await this.Util.getRoleList()
  },
  methods: {

    // 获取数据
    getData() {
      this.pageLoading = true
      this.Http.get("/admin/admin/detail", {
        id: this.id
      }).then(res => {
        this.pageLoading = false
        if (res.code === 1) {
          res.data.password = ""
          this.form = res.data
        } else {
          this.$message.error(res.msg)
        }
      })
    },

    // 提交更新
    onSubmit() {
      console.log('submit!', this.form);
      let url, data
      if (this.id) {
        url = "/admin/admin/update"
        data = {
          id: this.id,
          ...this.form
        }
      } else {
        url = '/admin/admin/create'
        data = this.form
      }
      this.Http.post(url, data).then(res => {
        if (res.code == 1) {
          this.getData()
          this.$message.success(res.msg)
        } else {
          this.$message.error(res.msg)
        }
      })
    },

    // 上传
    handleChange(info) {
      console.log(info.event)
      if (info.file.status === 'uploading') {
        this.uploadLoading = true;
        return;
      }
      if (info.file.status === 'done') {
        // Get this url from response in real world.
        getBase64(info.file.originFileObj, imageUrl => {
          this.imageUrl = imageUrl;
          this.uploadLoading = false;
        });
      }
    },

    // 上传前检测格式和大小
    beforeUpload(file) {
      const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png';
      if (!isJpgOrPng) {
        this.$message.error('You can only upload JPG file!');
      }
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isLt2M) {
        this.$message.error('Image must smaller than 2MB!');
      }
      return isJpgOrPng && isLt2M;
    },
  },
};
</script>
